package com.west.imclient.runnable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.west.imclient.handler.IHandler;
import com.west.imclient.parser.Parser;
import com.west.imclient.view.MeetingRoomView;

import imserverentity.entity.Protocol;


public class ReceiveRunnable implements Runnable{
	private MeetingRoomView meetingRoomView;
	private ObjectInputStream ois;
	
	public ReceiveRunnable(MeetingRoomView meetingRoomView, ObjectInputStream ois) {
		this.meetingRoomView = meetingRoomView;
		this.ois = ois;
		System.out.println("Receive OIS: " + ois + " " + this.ois);
	}
	
	public void run() {
		while(true) {
			try {
				Protocol protocol = (Protocol) ois.readObject();
				System.out.println("Receiver : " + protocol);
				
				//parse
				IHandler handler = Parser.parse(protocol, meetingRoomView);
				System.out.println(handler);
				if(handler != null) {
					handler.handle();
				} else {
					break;
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

}
